<?php
class News_model extends Model{

	var $_datagrid_name = 'dt1';
	
    function News_model(){
        parent::Model();       
    }
	function getdatasource($parrentid){ 
    	$choice_id = $this->_get_array_id($parrentid);
		$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$this->db->where_in('categoryid',$choice_id);
		$query=$this->db->get('news');
		$count = $query->num_rows();
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,categoryid');
    	$this->db->order_by('createddate DESC');
		$this->db->where_in('categoryid',$choice_id);
    	$query = $this->db->get('news');
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){
    		$dts[$i][1] = get_content('newscategory',$dts[$i][1])->name;
			$dts[$i][2] = get_content('news',$dts[$i][0])->title;
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
	function delete(){
		$code ='';
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
			$code = $this->id2code($id);
    		$this->_deleteimage($id);
    		delete_content('news',$id);
    		$this->db->delete('news',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			$this->_deleteimage($id[$i]);
    			delete_content('news',$id[$i]);
				$code = $this->id2code($id);
	    		$this->db->delete('news',array('id'=>$id[$i]));
    		}
    	}
		return $code;
    }
    function addnew($code){
    	if(FALSE!==$this->input->post('hidden_status')){
			$image_name = '';
    		$config['upload_path'] = $this->config->item('news_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    		}    		    		
    		$this->db->insert('news',array('image'=>$image_name,'categoryid'=>$this->input->post('cbo_category')));
    		set_content('news',$this->db->insert_id(),array('title'=>$this->input->post('txt_title'),'summary'=>$this->input->post('txt_summary'),'content'=>$this->input->post('txt_content')));
    		redirect("news/loadview/{$code}",'location');
    	}
    }
    function update(){
		$code ='';
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('news_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    			$this->_deleteimage($this->input->post('hidden_id'));
    		}
    		if(!empty($image_name)){
    			$this->db->set('image',$image_name);
    		}
			$code = $this->id2code($this->input->post('hidden_id'));
    		$this->db->where('id',$this->input->post('hidden_id'));
    		$this->db->update('news',array('categoryid'=>$this->input->post('cbo_category')));
    		set_content('news',$this->input->post('hidden_id'),array('title'=>$this->input->post('txt_title'),'summary'=>$this->input->post('txt_summary'),'content'=>$this->input->post('txt_content')));
    		redirect('news/loadview/'.$code);
    	}
		return $code;
    }
    function getdetail($id){
    	$query = $this->db->get_where('news',array('id'=>$id));
    	if($query->num_rows){
    		$news = $query->row();
    		$newscontent = get_content('news',$id);
    		$detail->id = $id;
    		$detail->image = $news->image;
    		$detail->createddate = $news->createddate;
    		$detail->categoryid = $news->categoryid;
    		$detail->title = $newscontent->title;
	    	$detail->summary = $newscontent->summary;
	    	$detail->content = $newscontent->content;
	    	return $detail;
    	}
    	else return FALSE;
    } 
    function getcategorylist($parentid){
    	$this->db->order_by('sortorder,id');
		$this->db->where('parentid',$parentid);
    	$query = $this->db->get('newscategory');
    	$category = array();
    	$res = $query->result();
    	for($i=0;$i<$query->num_rows();$i++){
    		$category[$i]->id = $res[$i]->id;
    		$category[$i]->name = get_content('newscategory',$res[$i]->id)->name;
    	}
    	return $category;
    }   
	function _deleteimage($id){
    	$res = $this->db->get_where('news',array('id'=>$id));
    	$image = $res->row()->image;
    	if(file_exists($this->config->item('news_img_path').$image)){
    		@unlink($this->config->item('news_img_path').$image);
    	}
    }
	
	function _get_array_id($parentid){
		$achild=array();
		$this->db->select('id');
		$query = $this->db->get_where('newscategory',array('parentid'=>$parentid));
		$result = $query->result();
		if(count($result)>0){
			for($i=0;$i<count($result);$i++){
				$achild[$i]=$result[$i]->id;
				}
		}else{
				$achild[0]=$parentid;
			 }
			return $achild;
		}
		
	
	// Input : code
	// Output : id
	function code2id($code){
		$this->db->select('id');
		$this->db->where('code',$code);
		$query = $this->db->get('newscategory');
		$row = $query->row();
		if(count($row)){
		return $row->id;
		} else {
			return NULL;
			}
	}
	// Input : $newcategory
	// Output : Code của parrentid lớn nhất.
	function id2code($news_id){
		$catid = $this->get_category_by_news_id($news_id);
		$parentid = $this->get_parentid($catid);
		$this->db->select('code');
		$this->db->where('id',$parentid);
		$query = $this->db->get('newscategory');
		$row = $query->row();
		if(count($row)){
		return $row->code;
		} else {
			return NULL;
			}
	}
	// Input categoryid
	// Output parentid  nếu categoryid đầu vào là parentid thì trả về chính nó
	function get_parentid($categoryid){
		$this->db->select('parentid');
		$this->db->where('id',$categoryid);
		$query = $this->db->get('newscategory');
		$row = $query->row();
		if($row->parentid == 0){
			return $categoryid;
		}else{
			return $this->get_parentid($row->parentid);
		}
	}
	// Input: news_id 
	// Output : categoryid
	function get_category_by_news_id($news_id){
		$query = $this->db->get_where('news',array('id'=>$news_id));
		$result = $query->result();
		return $result[0]->categoryid;
	}
		
}

/* End of file */